package com.ir.slassm.domain;

import java.util.Calendar;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ir.slassm.dao.SLMessageDAO;
import com.ir.slassm.vo.SLMessageVO;

public class SLMessage {
	Log logger = LogFactory.getLog(SLMessage.class.getName());
	private SLMessageDAO slMessageDAO;
	
	public static final String MESSAGE_ATTRIBUTE_SENDER_ID = "senderId";
	public static final String MESSAGE_ATTRIBUTE_RECEIVER_ID = "receiverId";
	public static final String MESSAGE_ATTRIBUTE_MESSAGE_HEADER = "messageHeader";
	public static final String MESSAGE_ATTRIBUTE_MESSAGE_BODY = "messageBody";
	
	public static final String MESSAGE_SENT_SUCCESS = "##SUCCESS##";
	public static final String MESSAGE_SENT_FAILED = "##FAILED##";
	public static final String MESSAGE_TOKEN_HEADER = "##HEADER##";
	public static final String MESSAGE_TOKEN_BODY = "##BODY##";
	public static final String MESSAGE_TOKEN_ROW = "##ROW##";
	
	public static final String MESSAGE_ERROR_SEND_VALIDATION = "101";
	public static final String MESSAGE_ERROR_RECEIVE_VALIDATION = "201";
	public static final String MESSAGE_ERROR_SPAM = "301";
	
	public SLMessage() {
		slMessageDAO = new SLMessageDAO();
	}
	
	public boolean verifyApprovedRequestor(String requestor) {
		boolean approved = false;
		
		//TODO: apply logic to verify apprved requestors
		approved = true;
		
		if(!approved) {
			logger.warn("Failed to approve requestor address from:" + requestor);
		}
		return approved;
	}
	
	public String clearExpiredMessages() {
		logger.info("clearExpiredMessages Enter");

		Calendar c = Calendar.getInstance();
		c.add(Calendar.HOUR_OF_DAY, -24); //1 Day ago 
		
		try {
			slMessageDAO.deleteByDataAgo(c.getTime());
		} catch (Throwable t) {
			return MESSAGE_SENT_FAILED + t.getMessage();
		}
		
		logger.info("clearExpiredMessages Exit");
		return MESSAGE_SENT_SUCCESS;
	}

	public String retrieveMessages(String senderId,
			String recieverId) {
		logger.info("retrieveMessages Enter");
		//TODO: validate sender/Reciever ids
		List<SLMessageVO> resultList = slMessageDAO.findBySenderAndReciever(senderId, recieverId);
		logger.info("retrieved " + resultList.size() + " results.");
		
		StringBuilder sb = new StringBuilder();
		
		for(SLMessageVO message : resultList) {
			sb.append(message.getMessageHeader());
			sb.append(MESSAGE_TOKEN_HEADER);
			sb.append(message.getMessageBody());
			sb.append(MESSAGE_TOKEN_BODY);
			sb.append(MESSAGE_TOKEN_ROW);
		}
		
		try {
			slMessageDAO.delete(resultList);
		} catch(Throwable t) { 
			return MESSAGE_SENT_FAILED + t.getMessage();
		}

		logger.info("retrieveMessages Exit");
		return sb.toString();
	}

	public String sendMessage(String fromURI, String senderId,
			String recieverId, String messageHeader, String messageBody) {
		logger.info("sendMessage Enter");
		String returnMessage;
		//TODO: Perform message validation as well as sender spam checker
		
		SLMessageVO message = SLMessageVO.createDefaultInstance();
		message.setSenderURI(fromURI);
		message.setSenderId(senderId);
		message.setRecievedId(recieverId);
		message.setMessageHeader(messageHeader);
		message.setMessageBody(messageBody);
		
		try {
			slMessageDAO.save(message);
			returnMessage = MESSAGE_SENT_SUCCESS;
		} catch(Throwable t) {
			returnMessage = MESSAGE_SENT_FAILED + t.getMessage();
		}
		
		logger.info("sendMessage Exit");
		return returnMessage;
	}
	
	/**
	 * All this check for is too many requests in a given time period from a given requester/sender/receiver combination
	 * @return
	 */
	public boolean isSpamCall(String fromURI, String senderId, String recieverId) {
		boolean isSpam = false;
		
		if(isSpam) {
			logger.warn("Potential spammer: fromURI:" + fromURI + " senderId: " + senderId + " recieverId: " + recieverId);
		}
		
		return isSpam;
	}
	
	
}
